﻿namespace DistrRTgen
{
    using System;
    using System.ComponentModel;
    using System.Runtime.CompilerServices;

    public class AsyncBindingList<T> : BindingList<T>
    {
        private ISynchronizeInvoke invoke;

        public AsyncBindingList(ISynchronizeInvoke invoke)
        {
            this.invoke = invoke;
        }

        protected override void OnListChanged(ListChangedEventArgs e)
        {
            if (this.invoke.InvokeRequired)
            {
                this.invoke.BeginInvoke(new ListChangedDelegate<T>(this.OnListChanged), new object[] { e });
            }
            else
            {
                base.OnListChanged(e);
            }
        }

        private delegate void ListChangedDelegate(ListChangedEventArgs e);
    }
}

